/**
 * date: 2011-5-18
 * author : Eason
 * description : 影片剪辑功能类.
 **/
package com.pickgliss.utils
{
	import com.pickgliss.bitmapEngin.cache.CacheFrame;
	import com.pickgliss.bitmapEngin.mode.BitmapMovieClipData;
	
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	
	public class MovieClipUtils
	{
		public function MovieClipUtils()
		{
		}
		public static function createBitmapMovieClipData(mc : MovieClip, id : String):BitmapMovieClipData
		{
			var data : BitmapMovieClipData = new BitmapMovieClipData();
			data.id = id;
			data.frames = new Vector.<CacheFrame>();
			data.labels = mc.currentLabels;
			
			var len : int = mc.totalFrames;
			var rect : Rectangle ;
			var matrix : Matrix = new Matrix();
			var registerX : int ;
			var registerY : int ;
			var colorTransform:ColorTransform = mc.transform.colorTransform ;
			for ( var i : int = 1 ; i <= len ; ++i )
			{
				mc.gotoAndPlay (i);
				rect = mc.getBounds(mc);
				matrix.identity();

				
				registerX = int(-rect.x);
				registerY = int(-rect.y);
				
				var k:int =  mc.numChildren;
				for (var j:int = 0 ; j<k; ++j)
				{
					var child:MovieClip =  mc.getChildAt(j) as MovieClip;
					if (child)
					{
						if ( child.currentFrame != child.totalFrames)
						{
							child.gotoAndStop(child.currentFrame+1);
						}else{
							child.gotoAndStop(1);
						}
					}
				}
				matrix.translate(registerX, registerY);
				var w : int = mc.width;
				var h : int = mc.height
				w = w > 0? w : 1;
				h = h > 0? h : 1;
				//trace(w, h);
				var bitmapData:BitmapData = new BitmapData(w, h, true, 0x00000000);
				bitmapData.draw(mc,matrix,colorTransform);
				data.frames.push(new CacheFrame(bitmapData,registerX,registerY));
			}
			return data;
		}
	}
}